package com.example.myweb.Controller.Auth;

import com.example.myweb.Common.ApiResponse;
import com.example.myweb.Domain.Entity.Article;
import com.example.myweb.Domain.Entity.User;

import com.example.myweb.Dto.ArticleDetailDTO;
import com.example.myweb.Dto.ArticleUpdateDTO;
import com.example.myweb.Dto.CreateArticleDTO;
import com.example.myweb.Dto.UpdateArticleDTO;
import com.example.myweb.Service.Auth.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> createArticle(
            @Valid @RequestBody CreateArticleDTO request,
            @AuthenticationPrincipal User currentUser) {

        Article createdArticle = articleService.createArticle(request, currentUser);
        Map<String, Object> responseData = Map.of("articleId", createdArticle.getArticleId());

        return new ResponseEntity<>(ApiResponse.success(responseData, "文章创建成功"), HttpStatus.CREATED);
    }

//    @GetMapping("/{id}")
//    public ResponseEntity<ApiResponse<ArticleDetailDTO>> getArticleById(@PathVariable Long id) {
//        ArticleDetailDTO articleDetail = articleService.getArticleById(id);
//        return ResponseEntity.ok(ApiResponse.success(articleDetail, "文章获取成功"));
//    }
        @GetMapping("/{id}")
        public ResponseEntity<ApiResponse<ArticleDetailDTO>> getArticleById(
                @PathVariable Long id,
                // 使用 @AuthenticationPrincipal 获取当前登录用户
                // 这个注解的好处是，如果用户未登录，currentUser 会自动为 null，不会报错
                @AuthenticationPrincipal User currentUser
        ) {
            // 将 currentUser 一起传递给 service 方法
            ArticleDetailDTO articleDetail = articleService.getArticleById(id, currentUser);
            return ResponseEntity.ok(ApiResponse.success(articleDetail, "文章获取成功"));
        }

            @GetMapping
            public ResponseEntity<ApiResponse<Page<ArticleDetailDTO>>> getAllArticles(
                    @RequestParam(defaultValue = "0") int page,
                    @RequestParam(defaultValue = "10") int size,
                    @RequestParam(defaultValue = "publishTime") String sortBy,
                    // 列表页也需要当前用户信息来判断点赞等状态
                    @AuthenticationPrincipal User currentUser
            ) {
                Pageable pageable = PageRequest.of(page, size, Sort.by(sortBy).descending());
                // 将 currentUser 传递给 service 方法
                Page<ArticleDetailDTO> articlePage = articleService.getAllArticles(pageable, currentUser);
                return ResponseEntity.ok(ApiResponse.success(articlePage, "文章列表获取成功"));
            }


    /**
     * 更新文章
     * PUT /articles/{id}
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<ArticleDetailDTO>> updateArticle(
            @PathVariable Long id,
            @Valid @RequestBody UpdateArticleDTO request, // <<< 使用 UpdateArticleDTO
            @AuthenticationPrincipal User currentUser) {

        ArticleDetailDTO updatedArticle = articleService.updateArticle(id, request, currentUser);
        return ResponseEntity.ok(ApiResponse.success(updatedArticle, "文章更新成功"));
    }
//    @GetMapping
//    public ResponseEntity<ApiResponse<Page<ArticleDetailDTO>>> getAllArticles(
//            @RequestParam(defaultValue = "0") int page,
//            @RequestParam(defaultValue = "10") int size,
//            @RequestParam(defaultValue = "publishTime") String sortBy // 允许按不同字段排序
//    ) {
//        Pageable pageable = PageRequest.of(page, size, Sort.by(sortBy).descending());
//        Page<ArticleDetailDTO> articlePage = articleService.getAllArticles(pageable);
//        return ResponseEntity.ok(ApiResponse.success(articlePage, "文章列表获取成功"));
//    }

    /**
     * 更新文章
     * PUT /articles/{id}
     */
//    @PutMapping("/{id}")
//    public ResponseEntity<ApiResponse<ArticleDetailDTO>> updateArticle(
//            @PathVariable Long id,
//            @Valid @RequestBody ArticleUpdateDTO request,
//            @AuthenticationPrincipal User currentUser) {
//
//        ArticleDetailDTO updatedArticle = articleService.updateArticle(id, request, currentUser);
//        return ResponseEntity.ok(ApiResponse.success(updatedArticle, "文章更新成功"));
//    }

    /**
     * 删除文章
     * DELETE /articles/{id}
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteArticle(
            @PathVariable Long id,
            @AuthenticationPrincipal User currentUser) {

        articleService.deleteArticle(id, currentUser);
        return ResponseEntity.ok(ApiResponse.success(null, "文章删除成功"));
    }
}